package com.ys.peony.ui.adapter;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ys.peony.R;
import com.ys.peony.app.BaseApplication;
import com.ys.peony.enums.BusinessDetailStatus;
import com.ys.peony.model.JiekuanrenModel;
import com.ys.peony.ui.view.zlistview.adapter.BaseSwipeAdapter;
import com.ys.peony.ui.view.zlistview.enums.DragEdge;
import com.ys.peony.ui.view.zlistview.enums.ShowMode;
import com.ys.peony.ui.view.zlistview.listener.SimpleSwipeListener;
import com.ys.peony.ui.view.zlistview.widget.ZSwipeItem;

import java.util.List;

public class PatchListAdapter extends BaseSwipeAdapter {
    protected static final String TAG = PatchListAdapter.class.getSimpleName();
    private Activity context;
    private List<JiekuanrenModel> data;
    private int patchFlag;

    public PatchListAdapter(Activity context, int patchFlag) {
        this.context = context;
        this.patchFlag = patchFlag;
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public JiekuanrenModel getItem(int position) {
        return data == null ? null : data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getSwipeLayoutResourceId(int position) {
        return R.id.swipe_item;
    }

    @Override
    public View generateView(int position, ViewGroup parent) {
        return context.getLayoutInflater().inflate(R.layout.patch_item_layout,
                parent, false);
    }

    @Override
    public void fillValues(final int position, final View convertView) {
        final ZSwipeItem swipeItem = (ZSwipeItem) convertView
                .findViewById(R.id.swipe_item);

        ImageView iconImg = (ImageView) convertView.findViewById(R.id.jiekuanren_item_icon);
        TextView nameTV = (TextView) convertView.findViewById(R.id.jiekuanren_name_tv);
        TextView carBranchTV = (TextView) convertView.findViewById(R.id.jiekuanren_car_tv);
        TextView clientClasTV = (TextView) convertView.findViewById(R.id.jiekuanren_kehu_tv);
        TextView carNumTV = (TextView) convertView.findViewById(R.id.jiekuanren_carnum_tv);
        TextView ywStatusTV = (TextView) convertView.findViewById(R.id.jiekuanren_status_tv);
        TextView ywNumTV = (TextView) convertView.findViewById(R.id.jiekuanren_yewunum_tv);

        final JiekuanrenModel item = data.get(position);
        if (item == null) {
            return;
        }

        if (patchFlag == 0) {
            iconImg.setImageResource(R.mipmap.repair);
        } else {
            iconImg.setImageResource(R.mipmap.repairback);
        }
        nameTV.setText(BaseApplication.getInstance().getResources().getString(R.string.name_lable) + item.getClientName());
        if (item.getVehicleList() != null && item.getVehicleList().size() > 0) {
            carBranchTV.setText(BaseApplication.getInstance().getResources().getString(R.string.jiekuanren_car) + item.getVehicleList().get(0).getBrand_name());
            carNumTV.setText(BaseApplication.getInstance().getResources().getString(R.string.jiekuanren_carnum) + item.getVehicleList().get(0).getPlate_number());
        } else {
            carBranchTV.setText(BaseApplication.getInstance().getResources().getString(R.string.jiekuanren_car) + "无");
            carNumTV.setText(BaseApplication.getInstance().getResources().getString(R.string.jiekuanren_carnum) + "无");
        }
        clientClasTV.setText(BaseApplication.getInstance().getResources().getString(R.string.jiekuanren_kehu) + item.getClientClassfyName());
        String patchStatus = "";
        if (TextUtils.isEmpty(item.getPatch_status())) {
            patchStatus = "未补件";
        } else if ("0".equals(item.getPatch_status())) {
            patchStatus = "待提交";
        } else if ("1".equals(item.getPatch_status())) {
            patchStatus = "已提交";
        } else {
            patchStatus = BusinessDetailStatus.getStatus(item.getLoanStatus());
        }
        ywStatusTV.setText("补件状态：" + patchStatus);
        ywNumTV.setText(BaseApplication.getInstance().getResources().getString(R.string.jiekuanren_yewu_num) + item.getSerialNumber());

        swipeItem.setShowMode(ShowMode.PullOut);
        swipeItem.setDragEdge(DragEdge.Right);

        swipeItem.addSwipeListener(new SimpleSwipeListener() {
            @Override
            public void onOpen(ZSwipeItem layout) {
//                LogUtil.i(TAG, "打开:" + position);
            }

            @Override
            public void onClose(ZSwipeItem layout) {
//                LogUtil.i(TAG, "关闭:" + position);
            }

            @Override
            public void onStartOpen(ZSwipeItem layout) {
//                LogUtil.i(TAG, "准备打开:" + position);
            }

            @Override
            public void onStartClose(ZSwipeItem layout) {
//                LogUtil.i(TAG, "准备关闭:" + position);
            }

            @Override
            public void onHandRelease(ZSwipeItem layout, float xvel, float yvel) {
//                LogUtil.i(TAG, "手势释放");
            }

            @Override
            public void onUpdate(ZSwipeItem layout, int leftOffset,
                                 int topOffset) {
//                LogUtil.i(TAG, "位置更新");
            }
        });

    }

    public void setData(List<JiekuanrenModel> date) {
        this.data = date;
        notifyDataSetChanged();
    }


}
